<!DOCTYPE html>
<html lang="">
    <head><meta charset='utf-8'>
<meta name='viewport' content='width=device-width, initial-scale=1'>
<script async src="//busuanzi.ibruce.info/busuanzi/2.3/busuanzi.pure.mini.js">
</script><meta name='description' content='846. 一手顺子 题目描述 Alice 手中有一把牌，她想要重新排列这些牌，分成若干组，使每一组的牌数都是 groupSize ，并且由 groupSize 张连续的牌组成。
给你一个整数数组 hand 其中 hand[i] 是写在第 i 张牌，和一个整数 groupSize 。如果她可能重新排列这些牌，返回 true ；否则，返回 false 。 示例：
输入：hand = [1,2,3,6,2,3,4,7,8], groupSize = 3输出：true解释：Alice 手中的牌可以被重新排列为 [1,2,3]，[2,3,4]，[6,7,8]。解题思路 哈希表&#43;堆 这是一道模拟牌组合题，由题目可知我们要让数组均匀的分成几组使每组的个数都是groupsize个并且要为连续的数字。
因数组中存在重复的元素，我们可以先用哈希表去存储每个不重复元素存在的个数，再使用小根堆存储数组中的每个元素。 然后我们可以每次从小根堆中弹出一个元素 a，通过条件判断元素 a是否可以作为组的起始元素。若可以那么就遍历获取[a,a&#43;groupsize)区间内的所有元素，若不存在也就证明无法组成我们想要的结果则返回false ,若元素存在那么将哈希表中的元素减一，当[a,a&#43;groupsize)都走完也就组成了一组元素。当我们把队列都清空时，没有返回false那就证明数组符合题意返回true。
代码示例 public boolean isNStraightHand(int[] hand, int m) { Map&amp;lt;Integer, Integer&amp;gt; map = new HashMap&amp;lt;&amp;gt;(); PriorityQueue&amp;lt;Integer&amp;gt; q = new PriorityQueue&amp;lt;&amp;gt;((a,b)-&amp;gt;a-b); for (int i : hand) { map.put(i, map.getOrDefault(i, 0) &#43; 1); q.'><title>一手顺子</title>

<link rel='canonical' href='http://example.org/post/%E4%B8%80%E6%89%8B%E9%A1%BA%E5%AD%90/'>

<link rel="stylesheet" href="/scss/style.min.css"><meta property='og:title' content='一手顺子'>
<meta property='og:description' content='846. 一手顺子 题目描述 Alice 手中有一把牌，她想要重新排列这些牌，分成若干组，使每一组的牌数都是 groupSize ，并且由 groupSize 张连续的牌组成。
给你一个整数数组 hand 其中 hand[i] 是写在第 i 张牌，和一个整数 groupSize 。如果她可能重新排列这些牌，返回 true ；否则，返回 false 。 示例：
输入：hand = [1,2,3,6,2,3,4,7,8], groupSize = 3输出：true解释：Alice 手中的牌可以被重新排列为 [1,2,3]，[2,3,4]，[6,7,8]。解题思路 哈希表&#43;堆 这是一道模拟牌组合题，由题目可知我们要让数组均匀的分成几组使每组的个数都是groupsize个并且要为连续的数字。
因数组中存在重复的元素，我们可以先用哈希表去存储每个不重复元素存在的个数，再使用小根堆存储数组中的每个元素。 然后我们可以每次从小根堆中弹出一个元素 a，通过条件判断元素 a是否可以作为组的起始元素。若可以那么就遍历获取[a,a&#43;groupsize)区间内的所有元素，若不存在也就证明无法组成我们想要的结果则返回false ,若元素存在那么将哈希表中的元素减一，当[a,a&#43;groupsize)都走完也就组成了一组元素。当我们把队列都清空时，没有返回false那就证明数组符合题意返回true。
代码示例 public boolean isNStraightHand(int[] hand, int m) { Map&amp;lt;Integer, Integer&amp;gt; map = new HashMap&amp;lt;&amp;gt;(); PriorityQueue&amp;lt;Integer&amp;gt; q = new PriorityQueue&amp;lt;&amp;gt;((a,b)-&amp;gt;a-b); for (int i : hand) { map.put(i, map.getOrDefault(i, 0) &#43; 1); q.'>
<meta property='og:url' content='http://example.org/post/%E4%B8%80%E6%89%8B%E9%A1%BA%E5%AD%90/'>
<meta property='og:site_name' content='我的技术小屋'>
<meta property='og:type' content='article'><meta property='article:section' content='Post' /><meta property='article:tag' content='哈希表' /><meta property='article:tag' content='双指针' /><meta property='article:tag' content='堆' /><meta property='article:published_time' content='2021-12-28T00:00:00&#43;00:00'/><meta property='article:modified_time' content='2021-12-28T00:00:00&#43;00:00'/><meta property='og:image' content='http://example.org/post/%E4%B8%80%E6%89%8B%E9%A1%BA%E5%AD%90/aa.png' />
<meta name="twitter:title" content="一手顺子">
<meta name="twitter:description" content="846. 一手顺子 题目描述 Alice 手中有一把牌，她想要重新排列这些牌，分成若干组，使每一组的牌数都是 groupSize ，并且由 groupSize 张连续的牌组成。
给你一个整数数组 hand 其中 hand[i] 是写在第 i 张牌，和一个整数 groupSize 。如果她可能重新排列这些牌，返回 true ；否则，返回 false 。 示例：
输入：hand = [1,2,3,6,2,3,4,7,8], groupSize = 3输出：true解释：Alice 手中的牌可以被重新排列为 [1,2,3]，[2,3,4]，[6,7,8]。解题思路 哈希表&#43;堆 这是一道模拟牌组合题，由题目可知我们要让数组均匀的分成几组使每组的个数都是groupsize个并且要为连续的数字。
因数组中存在重复的元素，我们可以先用哈希表去存储每个不重复元素存在的个数，再使用小根堆存储数组中的每个元素。 然后我们可以每次从小根堆中弹出一个元素 a，通过条件判断元素 a是否可以作为组的起始元素。若可以那么就遍历获取[a,a&#43;groupsize)区间内的所有元素，若不存在也就证明无法组成我们想要的结果则返回false ,若元素存在那么将哈希表中的元素减一，当[a,a&#43;groupsize)都走完也就组成了一组元素。当我们把队列都清空时，没有返回false那就证明数组符合题意返回true。
代码示例 public boolean isNStraightHand(int[] hand, int m) { Map&amp;lt;Integer, Integer&amp;gt; map = new HashMap&amp;lt;&amp;gt;(); PriorityQueue&amp;lt;Integer&amp;gt; q = new PriorityQueue&amp;lt;&amp;gt;((a,b)-&amp;gt;a-b); for (int i : hand) { map.put(i, map.getOrDefault(i, 0) &#43; 1); q."><meta name="twitter:card" content="summary_large_image">
    <meta name="twitter:image" content='http://example.org/post/%E4%B8%80%E6%89%8B%E9%A1%BA%E5%AD%90/aa.png' />
    <link rel="shortcut icon" href="/img/%e7%bc%85%e5%9b%a0%e7%8c%ab.png" />
<style>
    :root {
        --article-font-family: "Noto Serif SC", var(--base-font-family);
    }
</style>

<script> 
		(function () {
		    const customFont = document.createElement('link');
		    customFont.href = "https://fonts.googleapis.com/css2?family=Noto+Serif+SC:wght@300;700&display=swap"; 
		    customFont.type = "text/css";
		    customFont.rel = "stylesheet";
		
		    document.head.appendChild(customFont);
		}());
</script>
    </head>
    <body class="
    article-page has-toc
">
    <script>
        (function() {
            const colorSchemeKey = 'StackColorScheme';
            if(!localStorage.getItem(colorSchemeKey)){
                localStorage.setItem(colorSchemeKey, "auto");
            }
        })();
    </script><script>
    (function() {
        const colorSchemeKey = 'StackColorScheme';
        const colorSchemeItem = localStorage.getItem(colorSchemeKey);
        const supportDarkMode = window.matchMedia('(prefers-color-scheme: dark)').matches === true;

        if (colorSchemeItem == 'dark' || colorSchemeItem === 'auto' && supportDarkMode) {
            

            document.documentElement.dataset.scheme = 'dark';
        } else {
            document.documentElement.dataset.scheme = 'light';
        }
    })();
</script>
<div class="container main-container flex 
    
        extended
    
">
    
        <div id="article-toolbar">
            <a href="/" class="back-home">
                <svg xmlns="http://www.w3.org/2000/svg" class="icon icon-tabler icon-tabler-chevron-left" width="24" height="24" viewBox="0 0 24 24" stroke-width="2" stroke="currentColor" fill="none" stroke-linecap="round" stroke-linejoin="round">
  <path stroke="none" d="M0 0h24v24H0z"/>
  <polyline points="15 6 9 12 15 18" />
</svg>



                <span>Back</span>
            </a>
        </div>
    
<main class="main full-width">
    <article class="has-image main-article">
    <header class="article-header">
        <div class="article-image">
            <a href="/post/%E4%B8%80%E6%89%8B%E9%A1%BA%E5%AD%90/">
                <img src="/post/%E4%B8%80%E6%89%8B%E9%A1%BA%E5%AD%90/aa_hud3c20fccaaa9a9c72056f364d14b5f1f_1074620_800x0_resize_box_3.png"
                        srcset="/post/%E4%B8%80%E6%89%8B%E9%A1%BA%E5%AD%90/aa_hud3c20fccaaa9a9c72056f364d14b5f1f_1074620_800x0_resize_box_3.png 800w, /post/%E4%B8%80%E6%89%8B%E9%A1%BA%E5%AD%90/aa_hud3c20fccaaa9a9c72056f364d14b5f1f_1074620_1600x0_resize_box_3.png 1600w"
                        width="800" 
                        height="528" 
                        loading="lazy"
                        alt="Featured image of post 一手顺子" />
                
            </a>
        </div>
    

    <div class="article-details">
    
    <header class="article-category">
        
            <a href="/categories/%E6%AF%8F%E6%97%A5%E4%B8%80%E9%A2%98/" >
                每日一题
            </a>
        
    </header>
    

    <h2 class="article-title">
        <a href="/post/%E4%B8%80%E6%89%8B%E9%A1%BA%E5%AD%90/">一手顺子</a>
    </h2>

    

    
    <footer class="article-time">
        
            <div>
                <svg xmlns="http://www.w3.org/2000/svg" class="icon icon-tabler icon-tabler-calendar-time" width="56" height="56" viewBox="0 0 24 24" stroke-width="2" stroke="currentColor" fill="none" stroke-linecap="round" stroke-linejoin="round">
  <path stroke="none" d="M0 0h24v24H0z"/>
  <path d="M11.795 21h-6.795a2 2 0 0 1 -2 -2v-12a2 2 0 0 1 2 -2h12a2 2 0 0 1 2 2v4" />
  <circle cx="18" cy="18" r="4" />
  <path d="M15 3v4" />
  <path d="M7 3v4" />
  <path d="M3 11h16" />
  <path d="M18 16.496v1.504l1 1" />
</svg>
                <time class="article-time--published">Dec 28, 2021</time>
            </div>
        

        
            <div>
                <svg xmlns="http://www.w3.org/2000/svg" class="icon icon-tabler icon-tabler-clock" width="24" height="24" viewBox="0 0 24 24" stroke-width="2" stroke="currentColor" fill="none" stroke-linecap="round" stroke-linejoin="round">
  <path stroke="none" d="M0 0h24v24H0z"/>
  <circle cx="12" cy="12" r="9" />
  <polyline points="12 7 12 12 15 15" />
</svg>



                <time class="article-time--reading">
                    1 minute read
                </time>
            </div>
        
    </footer>
    
</div>
</header>

    <section class="article-content">
    <h1 id="846-一手顺子httpsleetcode-cncomproblemshand-of-straights"><a class="link" href="https://leetcode-cn.com/problems/hand-of-straights/"  target="_blank" rel="noopener"
    >846. 一手顺子</a></h1>
<h2 id="题目描述">题目描述</h2>
<p>Alice 手中有一把牌，她想要重新排列这些牌，分成若干组，使每一组的牌数都是 <code>groupSize</code> ，并且由 <code>groupSize</code> 张连续的牌组成。</p>
<p>给你一个整数数组 hand 其中 <code>hand[i]</code> 是写在第 i 张牌，和一个整数 <code>groupSize</code> 。如果她可能重新排列这些牌，返回 true ；否则，返回 false 。
<strong>示例</strong>：</p>
<pre tabindex="0"><code>输入：hand = [1,2,3,6,2,3,4,7,8], groupSize = 3
输出：true
解释：Alice 手中的牌可以被重新排列为 [1,2,3]，[2,3,4]，[6,7,8]。
</code></pre><h2 id="解题思路">解题思路</h2>
<h3 id="哈希表堆">哈希表+堆</h3>
<p>这是一道模拟牌组合题，由题目可知我们要让数组均匀的分成几组使每组的个数都是<strong>groupsize</strong>个并且要为<strong>连续</strong>的数字。</p>
<p>因数组中存在重复的元素，我们可以先用<strong>哈希表</strong>去存储每个不重复元素存在的个数，再使用<strong>小根堆</strong>存储数组中的每个元素。
然后我们可以每次从小根堆中弹出一个元素 a，通过条件判断元素 a是否可以作为组的起始元素。若可以那么就遍历获取[a,a+groupsize)区间内的所有元素，若不存在也就证明无法组成我们想要的结果则返回<code>false </code>,若元素存在那么将<strong>哈希表</strong>中的元素减一，当[a,a+groupsize)都走完也就组成了一组元素。当我们把队列都清空时，没有返回<code>false</code>那就证明数组符合题意返回<code>true</code>。</p>
<h3 id="代码示例">代码示例</h3>
<div class="highlight"><pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code class="language-java" data-lang="java"><span style="color:#66d9ef">public</span> <span style="color:#66d9ef">boolean</span> <span style="color:#a6e22e">isNStraightHand</span><span style="color:#f92672">(</span><span style="color:#66d9ef">int</span><span style="color:#f92672">[]</span> hand<span style="color:#f92672">,</span> <span style="color:#66d9ef">int</span> m<span style="color:#f92672">)</span> <span style="color:#f92672">{</span>
        Map<span style="color:#f92672">&lt;</span>Integer<span style="color:#f92672">,</span> Integer<span style="color:#f92672">&gt;</span> map <span style="color:#f92672">=</span> <span style="color:#66d9ef">new</span> HashMap<span style="color:#f92672">&lt;&gt;();</span>
        PriorityQueue<span style="color:#f92672">&lt;</span>Integer<span style="color:#f92672">&gt;</span> q <span style="color:#f92672">=</span> <span style="color:#66d9ef">new</span> PriorityQueue<span style="color:#f92672">&lt;&gt;((</span>a<span style="color:#f92672">,</span>b<span style="color:#f92672">)-&gt;</span>a<span style="color:#f92672">-</span>b<span style="color:#f92672">);</span>
        <span style="color:#66d9ef">for</span> <span style="color:#f92672">(</span><span style="color:#66d9ef">int</span> i <span style="color:#f92672">:</span> hand<span style="color:#f92672">)</span> <span style="color:#f92672">{</span>
            map<span style="color:#f92672">.</span><span style="color:#a6e22e">put</span><span style="color:#f92672">(</span>i<span style="color:#f92672">,</span> map<span style="color:#f92672">.</span><span style="color:#a6e22e">getOrDefault</span><span style="color:#f92672">(</span>i<span style="color:#f92672">,</span> 0<span style="color:#f92672">)</span> <span style="color:#f92672">+</span> 1<span style="color:#f92672">);</span>
            q<span style="color:#f92672">.</span><span style="color:#a6e22e">add</span><span style="color:#f92672">(</span>i<span style="color:#f92672">);</span>
        <span style="color:#f92672">}</span>
        <span style="color:#66d9ef">while</span> <span style="color:#f92672">(!</span>q<span style="color:#f92672">.</span><span style="color:#a6e22e">isEmpty</span><span style="color:#f92672">())</span> <span style="color:#f92672">{</span>
            <span style="color:#66d9ef">int</span> t <span style="color:#f92672">=</span> q<span style="color:#f92672">.</span><span style="color:#a6e22e">poll</span><span style="color:#f92672">();</span>
            <span style="color:#66d9ef">if</span> <span style="color:#f92672">(</span>map<span style="color:#f92672">.</span><span style="color:#a6e22e">get</span><span style="color:#f92672">(</span>t<span style="color:#f92672">)</span> <span style="color:#f92672">==</span> 0<span style="color:#f92672">)</span> <span style="color:#66d9ef">continue</span><span style="color:#f92672">;</span>
            <span style="color:#66d9ef">for</span> <span style="color:#f92672">(</span><span style="color:#66d9ef">int</span> i <span style="color:#f92672">=</span> 0<span style="color:#f92672">;</span> i <span style="color:#f92672">&lt;</span> m<span style="color:#f92672">;</span> i<span style="color:#f92672">++)</span> <span style="color:#f92672">{</span>
                <span style="color:#66d9ef">int</span> cnt <span style="color:#f92672">=</span> map<span style="color:#f92672">.</span><span style="color:#a6e22e">getOrDefault</span><span style="color:#f92672">(</span>t <span style="color:#f92672">+</span> i<span style="color:#f92672">,</span> 0<span style="color:#f92672">);</span>
                <span style="color:#66d9ef">if</span> <span style="color:#f92672">(</span>cnt <span style="color:#f92672">==</span> 0<span style="color:#f92672">)</span> <span style="color:#66d9ef">return</span> <span style="color:#66d9ef">false</span><span style="color:#f92672">;</span>
                map<span style="color:#f92672">.</span><span style="color:#a6e22e">put</span><span style="color:#f92672">(</span>t <span style="color:#f92672">+</span> i<span style="color:#f92672">,</span> cnt <span style="color:#f92672">-</span> 1<span style="color:#f92672">);</span>
            <span style="color:#f92672">}</span>
        <span style="color:#f92672">}</span>
        <span style="color:#66d9ef">return</span> <span style="color:#66d9ef">true</span><span style="color:#f92672">;</span>

    <span style="color:#f92672">}</span>
</code></pre></div><p><figure 
	
		class="gallery-image" 
		style="
			flex-grow: 190; 
			flex-basis: 457px"
	>
	<a href="/post/%E4%B8%80%E6%89%8B%E9%A1%BA%E5%AD%90/dd.png" data-size="892x468">
		<img src="/post/%E4%B8%80%E6%89%8B%E9%A1%BA%E5%AD%90/dd.png"
			width="892"
			height="468"
			srcset="/post/%E4%B8%80%E6%89%8B%E9%A1%BA%E5%AD%90/dd_hufa1ff9a9ee7ff29557d2cdc18f0fbef5_23614_480x0_resize_box_3.png 480w, /post/%E4%B8%80%E6%89%8B%E9%A1%BA%E5%AD%90/dd_hufa1ff9a9ee7ff29557d2cdc18f0fbef5_23614_1024x0_resize_box_3.png 1024w"
			loading="lazy"
			>
	</a>
	
</figure></p>
<h3 id="双指针排序">双指针+排序</h3>
<p>上面讲了哈希表和小根堆来模拟这个组合的过程，试想有没有可以用占用空间更小的方法来达到这个模拟的过程呢？</p>
<p>首先我们可以将原数组进行升序排序，然后通过循环使去枚举出发点经过判断成功的出发点a，定义一个变量<code>count=0</code>通过count去找我们后续元素的值。</p>
<p>再通过内层循环以<code>a+1</code>的位置开始，在数组中找到[a+1,a+groupsize)位置的元素，又因为我们利用count去作为满足条件，count的值最大应为<code>(a+groupsize)-(a+1)=group-1</code>。我们可以通过条件<code>hand[j]-hand[i]=count+1</code>找到我们想要的元素后让<code>count++</code>并且让该元素在数组中的值置为-1，当每次内层循环遍历完，我们可以通过判断<code>count</code>是否等于<code>groupsize-1</code>来确定是否找到一组合规的元素。</p>
<h3 id="代码实现">代码实现</h3>
<div class="highlight"><pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code class="language-java" data-lang="java"><span style="color:#66d9ef">public</span> <span style="color:#66d9ef">static</span> <span style="color:#66d9ef">boolean</span> <span style="color:#a6e22e">isNStraightHand</span><span style="color:#f92672">(</span><span style="color:#66d9ef">int</span><span style="color:#f92672">[]</span> hand<span style="color:#f92672">,</span> <span style="color:#66d9ef">int</span> groupSize<span style="color:#f92672">)</span> <span style="color:#f92672">{</span>
        Arrays<span style="color:#f92672">.</span><span style="color:#a6e22e">sort</span><span style="color:#f92672">(</span>hand<span style="color:#f92672">);</span>
        <span style="color:#66d9ef">for</span> <span style="color:#f92672">(</span><span style="color:#66d9ef">int</span> i <span style="color:#f92672">=</span> 0<span style="color:#f92672">;</span> i <span style="color:#f92672">&lt;</span> hand<span style="color:#f92672">.</span><span style="color:#a6e22e">length</span><span style="color:#f92672">;</span> i<span style="color:#f92672">++)</span> <span style="color:#f92672">{</span>
        <span style="color:#75715e">//以每个元素作为起始位置
</span><span style="color:#75715e"></span>            <span style="color:#66d9ef">if</span> <span style="color:#f92672">(</span>hand<span style="color:#f92672">[</span>i<span style="color:#f92672">]&lt;</span>0<span style="color:#f92672">){</span>
                <span style="color:#66d9ef">continue</span><span style="color:#f92672">;</span>
            <span style="color:#f92672">}</span>
            <span style="color:#66d9ef">int</span> cont<span style="color:#f92672">=</span>0<span style="color:#f92672">;</span>
            <span style="color:#75715e">//内层循环去继续往下走
</span><span style="color:#75715e"></span>            <span style="color:#66d9ef">for</span> <span style="color:#f92672">(</span><span style="color:#66d9ef">int</span> j <span style="color:#f92672">=</span> i<span style="color:#f92672">+</span>1<span style="color:#f92672">;</span> j <span style="color:#f92672">&lt;</span> hand<span style="color:#f92672">.</span><span style="color:#a6e22e">length</span> <span style="color:#f92672">&amp;&amp;</span>cont<span style="color:#f92672">!=</span>groupSize<span style="color:#f92672">-</span>1<span style="color:#f92672">;</span> j<span style="color:#f92672">++)</span> <span style="color:#f92672">{</span>
                <span style="color:#66d9ef">if</span> <span style="color:#f92672">(</span>hand<span style="color:#f92672">[</span>j<span style="color:#f92672">]-</span>hand<span style="color:#f92672">[</span>i<span style="color:#f92672">]==</span>cont<span style="color:#f92672">+</span>1<span style="color:#f92672">){</span>
                    cont<span style="color:#f92672">++;</span>
                    <span style="color:#75715e">//将j位置处的元素置为-1让他不能以头开始
</span><span style="color:#75715e"></span>                    hand<span style="color:#f92672">[</span>j<span style="color:#f92672">]=-</span>1<span style="color:#f92672">;</span>
                <span style="color:#f92672">}</span>
            <span style="color:#f92672">}</span>
            <span style="color:#66d9ef">if</span> <span style="color:#f92672">(</span>cont <span style="color:#f92672">!=</span> groupSize <span style="color:#f92672">-</span> 1<span style="color:#f92672">)</span> <span style="color:#66d9ef">return</span> <span style="color:#66d9ef">false</span><span style="color:#f92672">;</span>
        <span style="color:#f92672">}</span>
        <span style="color:#66d9ef">return</span> <span style="color:#66d9ef">true</span><span style="color:#f92672">;</span>
    <span style="color:#f92672">}</span>
</code></pre></div><p><figure 
	
		class="gallery-image" 
		style="
			flex-grow: 179; 
			flex-basis: 431px"
	>
	<a href="/post/%E4%B8%80%E6%89%8B%E9%A1%BA%E5%AD%90/ee.png" data-size="934x519">
		<img src="/post/%E4%B8%80%E6%89%8B%E9%A1%BA%E5%AD%90/ee.png"
			width="934"
			height="519"
			srcset="/post/%E4%B8%80%E6%89%8B%E9%A1%BA%E5%AD%90/ee_hu3fe99909e35bb2342a1da521a63bf6c0_32830_480x0_resize_box_3.png 480w, /post/%E4%B8%80%E6%89%8B%E9%A1%BA%E5%AD%90/ee_hu3fe99909e35bb2342a1da521a63bf6c0_32830_1024x0_resize_box_3.png 1024w"
			loading="lazy"
			>
	</a>
	
</figure></p>

</section>


    <footer class="article-footer">
    
    <section class="article-tags">
        
            <a href="/tags/%E5%93%88%E5%B8%8C%E8%A1%A8/">哈希表</a>
        
            <a href="/tags/%E5%8F%8C%E6%8C%87%E9%92%88/">双指针</a>
        
            <a href="/tags/%E5%A0%86/">堆</a>
        
    </section>


    </footer>


    
</article>

    <aside class="related-contents--wrapper">
    
    
</aside>

     
    
        
    

    <script src="https://s3.pstatp.com/cdn/expire-1-M/jquery/3.2.1/jquery.min.js"></script>
        <script>
        function color_tags() {
            var colorArr = ["#428BCA", "#AEDCAE", "#ECA9A7", "#DA99FF", "#FFB380", "#D9B999"];
            $('.tagCloud-tags a').each(function () {
                try {
                    tagsColor = colorArr[Math.floor(Math.random() * colorArr.length)];
                    $(this).css("background", tagsColor); 
                }
                catch (err) { }
            });
        }

        $(document).ready(function () {
            color_tags()
        });
    </script>
    

    

    

    

    

    

    

    

    

    

    

    

    

    

    

    

    

    

    

    

<footer class="site-footer">
<span id="busuanzi_container_site_uv">
    本站总访问量<span id="busuanzi_value_site_uv"></span>次
</span>
    <section class="copyright">
        &copy; 
        
        2022zp妙妙屋·<i class="fas fa-bell"></i> <a id="days">0</a>Days<br>共叭叭了 3682字·共 16篇文章</br><span><p>
    </section>
    <div style="width:300px;margin:0 auto; padding:20px 0;">
    <a href="https://beian.miit.gov.cn/，" rel="external nofollow" target="_blank"><p style="float:left;height:20px;line-height:20px;margin: 0px 0px 0px 25px; color:#939393;">京ICP备2022000201号-1</p></a></span>
		 		<a target="_blank" href="http://www.beian.gov.cn/portal/registerSystemInfo?recordcode=11011102001825" style="display:inline-block;text-decoration:none;height:20px;line-height:20px;"><img src="/beian.png" style="float:left;"/><p style="float:left;height:20px;line-height:20px;margin: 0px 0px 0px 5px; color:#939393;">京公网安备 11011102001825号</p></a>
	</div>
    <section class="powerby">
        Built with <a href="https://gohugo.io/" target="_blank" rel="noopener">Hugo</a> <br />
        Theme <b><a href="https://github.com/CaiJimmy/hugo-theme-stack" target="_blank" rel="noopener" data-version="3.5.0">Stack</a></b> designed by <a href="www.zpzp.vip" target="_blank" rel="noopener">ZP</a>
    </section>
</footer>



    
<div class="pswp" tabindex="-1" role="dialog" aria-hidden="true">

    
    <div class="pswp__bg"></div>

    
    <div class="pswp__scroll-wrap">

        
        <div class="pswp__container">
            <div class="pswp__item"></div>
            <div class="pswp__item"></div>
            <div class="pswp__item"></div>
        </div>

        
        <div class="pswp__ui pswp__ui--hidden">

            <div class="pswp__top-bar">

                

                <div class="pswp__counter"></div>

                <button class="pswp__button pswp__button--close" title="Close (Esc)"></button>

                <button class="pswp__button pswp__button--share" title="Share"></button>

                <button class="pswp__button pswp__button--fs" title="Toggle fullscreen"></button>

                <button class="pswp__button pswp__button--zoom" title="Zoom in/out"></button>

                
                
                <div class="pswp__preloader">
                    <div class="pswp__preloader__icn">
                        <div class="pswp__preloader__cut">
                            <div class="pswp__preloader__donut"></div>
                        </div>
                    </div>
                </div>
            </div>

            <div class="pswp__share-modal pswp__share-modal--hidden pswp__single-tap">
                <div class="pswp__share-tooltip"></div>
            </div>

            <button class="pswp__button pswp__button--arrow--left" title="Previous (arrow left)">
            </button>

            <button class="pswp__button pswp__button--arrow--right" title="Next (arrow right)">
            </button>

            <div class="pswp__caption">
                <div class="pswp__caption__center"></div>
            </div>

        </div>

    </div>

</div><script 
                src="https://cdn.jsdelivr.net/npm/photoswipe@4.1.3/dist/photoswipe.min.js"integrity="sha256-ePwmChbbvXbsO02lbM3HoHbSHTHFAeChekF1xKJdleo="crossorigin="anonymous"
                defer="true"
                >
            </script><script 
                src="https://cdn.jsdelivr.net/npm/photoswipe@4.1.3/dist/photoswipe-ui-default.min.js"integrity="sha256-UKkzOn/w1mBxRmLLGrSeyB4e1xbrp4xylgAWb3M42pU="crossorigin="anonymous"
                defer="true"
                >
            </script><link 
                rel="stylesheet" 
                href="https://cdn.jsdelivr.net/npm/photoswipe@4.1.3/dist/default-skin/default-skin.css"integrity="sha256-c0uckgykQ9v5k&#43;IqViZOZKc47Jn7KQil4/MP3ySA3F8="crossorigin="anonymous"
            ><link 
                rel="stylesheet" 
                href="https://cdn.jsdelivr.net/npm/photoswipe@4.1.3/dist/photoswipe.css"integrity="sha256-SBLU4vv6CA6lHsZ1XyTdhyjJxCjPif/TRkjnsyGAGnE="crossorigin="anonymous"
            >

            </main>
    
        <aside class="sidebar right-sidebar sticky">
            <section class="widget archives">
                <div class="widget-icon">
                    <svg xmlns="http://www.w3.org/2000/svg" class="icon icon-tabler icon-tabler-hash" width="24" height="24" viewBox="0 0 24 24" stroke-width="2" stroke="currentColor" fill="none" stroke-linecap="round" stroke-linejoin="round">
  <path stroke="none" d="M0 0h24v24H0z"/>
  <line x1="5" y1="9" x2="19" y2="9" />
  <line x1="5" y1="15" x2="19" y2="15" />
  <line x1="11" y1="4" x2="7" y2="20" />
  <line x1="17" y1="4" x2="13" y2="20" />
</svg>



                </div>
                <h2 class="widget-title section-title">Table of contents</h2>
                
                <div class="widget--toc">
                    <nav id="TableOfContents">
  <ul>
    <li><a href="#题目描述">题目描述</a></li>
    <li><a href="#解题思路">解题思路</a>
      <ul>
        <li><a href="#哈希表堆">哈希表+堆</a></li>
        <li><a href="#代码示例">代码示例</a></li>
        <li><a href="#双指针排序">双指针+排序</a></li>
        <li><a href="#代码实现">代码实现</a></li>
      </ul>
    </li>
  </ul>
</nav>
                </div>
            </section>
        </aside>
    

        </div>
        <script 
                src="https://cdn.jsdelivr.net/npm/node-vibrant@3.1.5/dist/vibrant.min.js"integrity="sha256-5NovOZc4iwiAWTYIFiIM7DxKUXKWvpVEuMEPLzcm5/g="crossorigin="anonymous"
                defer="false"
                >
            </script><script type="text/javascript" src="/ts/main.js" defer></script>
<script>
    (function () {
        const customFont = document.createElement('link');
        customFont.href = "https://fonts.googleapis.com/css2?family=Lato:wght@300;400;700&display=swap";

        customFont.type = "text/css";
        customFont.rel = "stylesheet";

        document.head.appendChild(customFont);
    }());
</script>

<script
    src="https://cdn.jsdelivr.net/gh/zhixuan2333/gh-blog@v0.1.0/js/ribbon.min.js"
    integrity="sha384-UEK8ZiP3VgFNP8KnKMKDmd4pAUAOJ59Y2Jo3ED2Z5qKQf6HLHovMxq7Beb9CLPUe"
    crossorigin="anonymous"
    size="300"
    alpha="0.6"
    zindex="-1"
    defer
></script>
<script
    src="https://cdn.jsdelivr.net/gh/zhixuan2333/gh-blog@v0.1.0/js/nprogress.min.js"
    integrity="sha384-bHDlAEUFxsRI7JfULv3DTpL2IXbbgn4JHQJibgo5iiXSK6Iu8muwqHANhun74Cqg"
    crossorigin="anonymous"
></script>
<link
    rel="stylesheet"
    href="https://cdn.jsdelivr.net/gh/zhixuan2333/gh-blog@v0.1.0/css/nprogress.css"
    integrity="sha384-KJyhr2syt5+4M9Pz5dipCvTrtvOmLk/olWVdfhAp858UCa64Ia5GFpTN7+G4BWpE"
    crossorigin="anonymous"
/>
<script src="/js/snow.js"></script>
<script>
    NProgress.start();
    document.addEventListener("readystatechange", () => {
        if (document.readyState === "interactive") NProgress.inc(0.8);
        if (document.readyState === "complete") NProgress.done();
    });
</script>

    </body>
</html>
